package com.kgdev.cweb.mbean.store.order;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.kgdev.cweb.beans.prod.impl.OrderBean;
import com.kgdev.cweb.entities.prod.managed.Order;
import com.kgdev.cweb.mbean.global.session.LoginBean;

@ManagedBean
@ViewScoped
public class OrderList implements Serializable {

	@EJB
	OrderBean orderBean;

	@ManagedProperty(value = "#{loginBean}")
	LoginBean loginBean;

	public void setLoginBean(LoginBean loginBean) {
		this.loginBean = loginBean;
	}

	private List<Order> orders;

	public List<Order> getOrders() {
		return orders;
	}

	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}

	private static final long serialVersionUID = 1L;

	@PostConstruct
	public void init() {
		initOrders();
	}

	public void initOrders() {
		List<Order> allOrders = orderBean.findAll();
		orders = new ArrayList<Order>();

		for (Order order : allOrders) {

			if (order.getClient().equals(loginBean.getAutenticatedClient())) {
				orders.add(order);
			}
		}
	}

	public String viewOrder(Integer orderId) {
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
				.put("orderViewId", orderId);
		return "toOrderView";
	}
}
